package cn.school.service.impl;

import cn.hutool.jwt.JWT;
import cn.hutool.jwt.JWTUtil;
import cn.school.mapper.ExamSessionMapper;
import cn.school.mapper.TeacherMapper;
import cn.school.pojo.ExamSession;
import cn.school.pojo.Teacher;
import cn.school.service.ExamSessionService;

import cn.school.vo.ResultVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
* @author Lenovo
* @description 针对表【exam_session】的数据库操作Service实现
* @createDate 2025-06-12 11:21:02
*/
@Service
public class ExamSessionServiceImpl extends ServiceImpl<ExamSessionMapper, ExamSession>
    implements ExamSessionService {
    @Resource
    private TeacherMapper teacherMapper;
    @Resource
    private ExamSessionMapper examSessionMapper;
    @Override
    public String addExamSession(String examSessionName, HttpServletRequest request) {
        String token = request.getHeader("token");
        JWT jwt = JWTUtil.parseToken(token);
        Object id = jwt.getPayload("id");
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",id);
        Teacher teacher = teacherMapper.selectOne(queryWrapper);
        if (teacher == null){
            return "您当前没有权限";
        }else {
            ExamSession examSession = new ExamSession();
            examSession.setSessionTitle(examSessionName);
            examSession.setIsDel(0);
            this.save(examSession);
            return "添加成功";
        }
    }

    @Override
    public String deleteExamSession(Integer examSessionId, HttpServletRequest request) {
        String token = request.getHeader("token");
        JWT jwt = JWTUtil.parseToken(token);
        Object id = jwt.getPayload("id");
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",id);
        Teacher teacher = teacherMapper.selectOne(queryWrapper);
        if (teacher == null){
            return "您当前没有权限";
        }else {
            QueryWrapper<ExamSession> examSessionQueryWrapper=new QueryWrapper<>();
            examSessionQueryWrapper.eq("id",examSessionId);
            ExamSession examSession = examSessionMapper.selectOne(examSessionQueryWrapper);
            examSession.setIsDel(1);
            this.updateById(examSession);
            return "添加成功";
        }
    }

    @Override
    public ResultVo findExamSessionList() {
        QueryWrapper<ExamSession> examSessionQueryWrapper=new QueryWrapper<>();
        examSessionQueryWrapper.eq("is_del",0);
        List<ExamSession> list = examSessionMapper.selectList(examSessionQueryWrapper);
        return ResultVo.ok(list);
    }
}




